import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0;i<n;i++){
            arr[i] = in.nextInt();
        }
        int count = 0;
        for(int i = 0;i<n;i++){
            int j = i+3;
            if(j <= n && fun(Arrays.copyOfRange(arr,i,j))){
                while(j <= n && fun(Arrays.copyOfRange(arr,i,j))){
                    count++;
                    j++;
                }
            }

            i++;
        }
        arr = reverse(arr);
        for(int i = 0;i<n;i++){
            int j = i+3;
            if(j <= n && fun(Arrays.copyOfRange(arr,i,j))){
                while(j <= n && fun(Arrays.copyOfRange(arr,i,j))){
                    count++;
                    j++;
                }
            }

            i++;
        }
        System.out.println(count);
    }
    public static boolean fun(int[] arr){
        Arrays.sort(arr);
        if(arr[0] != arr[1]){
            for(int i = 1;i<arr.length-1;i++){
                if(arr[i] != arr[i+1]){
                    return false;
                }
            }
            return true;
        }
        if(arr[arr.length-1] != arr[arr.length-2]){
            for(int i = 0;i<arr.length-2;i++){
                if(arr[i] != arr[i+1]){
                    return false;
                }
            }
            return true;
        }
        return false;
    }
    public static boolean same(int[] arr){
        for(int i = 0;i<arr.length-1;i++){
            if(arr[i] != arr[i+1]){
                return false;
            }
        }
        return true;
    }
    public static int[] reverse(int[] arr){
        int left = 0;
        int right = arr.length-1;
        while (left < right){
            int tmp = arr[left];
            arr[left++] = arr[right];
            arr[right--] = tmp;
        }
        return arr;
    }
}